
var docEl = document.documentElement;
var clientWidth = docEl.clientWidth;

(function flexible(window, document) {

    function setRemUnit() {
        docEl.style.fontSize = (clientWidth / 750) * 100 + "px";
        var scaledFontSize = parseInt(window.getComputedStyle(document.querySelector("html"), null).getPropertyValue("font-size"));
        if (parseInt(docEl.style.fontSize) !== scaledFontSize) {
            var val = (clientWidth / 750) * 100;
            docEl.style.fontSize = val * val / scaledFontSize + "px"
        }
    }

    setRemUnit();
    window.addEventListener("resize", setRemUnit);
    window.addEventListener("pageshow", function (e) {
        if (e.persisted) {
            setRemUnit()
        }
    })
}(window, document));

function jssdk() {
    var param = {
        "url": location.href.split('#')[0],
        "method": "getJssdkWechatConfig"
    };
    var url = this.getDomain()+"/api/getJssdkWechatConfig";
    $.ajax({
        url: url,
        type: "POST",
        async: true,
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(param),
        success: function (data) {
            if (data.resultCode == "000000") {
                wx.config({
                    debug: false,
                    appId: data.data.appId,
                    timestamp: data.data.timestamp,
                    nonceStr: data.data.nonceStr,
                    signature: data.data.signature,
                    jsApiList: ['checkJsApi', 'onMenuShareTimeline',
                        'onMenuShareAppMessage', 'onMenuShareQQ',
                        'onMenuShareWeibo', 'hideMenuItems',
                        'showMenuItems', 'hideAllNonBaseMenuItem',
                        'showAllNonBaseMenuItem', 'translateVoice',
                        'startRecord', 'stopRecord', 'onRecordEnd',
                        'playVoice', 'pauseVoice', 'stopVoice',
                        'uploadVoice', 'downloadVoice', 'chooseImage',
                        'previewImage', 'uploadImage', 'downloadImage',
                        'getNetworkType', 'openLocation', 'getLocation',
                        'hideOptionMenu', 'showOptionMenu', 'closeWindow',
                        'scanQRCode', 'chooseWXPay',
                        'openProductSpecificView', 'addCard', 'chooseCard',
                        'openCard'],
                    openTagList: ['wx-open-launch-weapp'] // 可选，需要使用的开放标签列表，例如['wx-open-launch-app']
                });
            }

        }
    });
}



wx.ready(function () {
    // config信息验证后会执行ready方法，所有接口调用都必须在config接口获得结果之后，config是一个客户端的异步操作，所以如果需要在页面加载时就调用相关接口，则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口，则可以直接调用，不需要放在ready函数中
});

wx.error(function (res) {
    // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名
});

function getDomain(){
    var curWwwPath=window.document.location.href;
    var pathName=window.document.location.pathname;
    var pos=curWwwPath.indexOf(pathName);
    var localhostPath=curWwwPath.substring(0,pos);
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
    return localhostPath+projectName;
}
